package com.supercube.sport.yoga.controller;

import com.supercube.common.controller.BaseController;
import com.supercube.common.utils.StringUtils;
import com.supercube.sport.yoga.domain.SalaryDesignDTO;
import com.supercube.sport.yoga.service.SalaryDesignService;
import com.supercube.sport.yoga.utils.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/salaryDesign")
@Api(tags = "薪酬设置")
public class SalaryDesignController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    // 薪酬梯度
    @Autowired
    private SalaryDesignService salaryDesignService;

    @ApiOperation(value = "保存薪酬设置信息", notes = "保存薪酬设置信息")
    @PostMapping("/save")
    public ResultInfo saveSalaryDesign(@RequestBody SalaryDesignDTO dto) {
        if (StringUtils.isNotEmpty(dto.getId())) {
            SalaryDesignDTO salaryDesignDTO = salaryDesignService.querySalaryDesignById(dto.getId());
            if (salaryDesignDTO == null) {
                return ResultInfo.failed("修改失败");
            }
            salaryDesignService.update(dto);
        } else {
            SalaryDesignDTO salaryDesignDTO = salaryDesignService.querySalaryDesignByName(dto.getName());
            if (salaryDesignDTO != null) {
                ResultInfo.failed("不可重名");
            }
            salaryDesignService.save(dto);
        }
        return ResultInfo.success("保存成功");
    }

    @ApiOperation(value = "查询薪酬设置信息列表，根据id", notes = "查询薪酬设置信息列表，根据id")
    @GetMapping("/querySalaryDesignById")
    public ResultInfo querySalaryDesignById(@RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return ResultInfo.validateFailed();
        }
        SalaryDesignDTO salaryDesignDTO = salaryDesignService.querySalaryDesignById(id);
        return ResultInfo.success(salaryDesignDTO);
    }

    @ApiOperation(value = "查询薪酬设置信息列表，根据类型", notes = "查询薪酬设置信息列表，根据类型")
    @GetMapping("/querySalaryDesignList")
    public ResultInfo querySalaryDesignList(@RequestParam("salaryType") String salaryType) {
        if (StringUtils.isEmpty(salaryType)) {
            return ResultInfo.validateFailed();
        }
        List<SalaryDesignDTO> salaryDesignDTOS = salaryDesignService.querySalaryDesignList(salaryType);
        return ResultInfo.success(salaryDesignDTOS);
    }

    @ApiOperation(value = "删除薪酬设置信息", notes = "删除薪酬设置信息")
    @GetMapping("/delete")
    public ResultInfo deleteSalaryDesignById(@RequestParam("id") String id) {
        if (isEmpty(id)) {
            return ResultInfo.failed("删除失败，id不能为空");
        }
        salaryDesignService.delete(id);
        return ResultInfo.success("删除成功");
    }

    @ApiOperation(value = "修改薪酬设置信息", notes = "修改薪酬设置信息")
    @PostMapping("/update")
    public ResultInfo updateVenueInfo(@RequestBody SalaryDesignDTO dto) {
        SalaryDesignDTO salaryDesignDTO = salaryDesignService.querySalaryDesignById(dto.getId());
        if (salaryDesignDTO == null) {
            return ResultInfo.failed("修改失败");
        }
        salaryDesignService.update(dto);
        return ResultInfo.success("修改成功");
    }
}
